package com.hengpeng.msls.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.hengpeng.common.configuration.ConfigProvider;
import com.hengpeng.common.configuration.ConfigProviderFactory;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.en.RewardRedPacketStatus;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.AccountLog;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.msls.util.WeixinUtil;
import com.hengpeng.msls.vo.SessionMember;

/**
 * 账户中心控制器
 * 
 * @author lugaofei 20151029
 *
 */
@Controller
@RequestMapping(value = "/member")
public class MemberCenterController extends BaseController {
    
    public final static ConfigProvider<String> cp = ConfigProviderFactory
            .getInstance(Constant.HP_COMMON_PROPERTIES_FILEPATH);

    public String appId = cp.getConfigByPrimaryKey("weixin.appId");

    public String appSecret = cp.getConfigByPrimaryKey("weixin.appSecret");
    
    public String h5domain = cp.getConfigByPrimaryKey("weixin.h5domain");
    /**
     * 个人中心
     */
    @RequestMapping(value = "/center")
    public String memberCenter(HttpServletRequest request, ModelMap map) {
        SessionMember session = getSessionMember(request);
        if (session == null) {
            return "/user/login/index.jsp";
        }
        UserInfo user = serviceFactory.getUserInfoService().getUserInfoById(session.getUser().getId());
        Account account = serviceFactory.getAccountService().getAccount(user.getId());
        // 红包总额
        Long rewardRedPackageMoney = serviceFactory.getRedPacketService().countRedPacket(user.getId(),new RewardRedPacketStatus[] {RewardRedPacketStatus.CANUSE});
        map.put("user", user);
        map.put("account", account);
        map.put("redAmount", rewardRedPackageMoney);
        return "/page/account/center.jsp";
    }

    /**
     * 邀请好友页面显示
     */
    @RequestMapping(value = "invitepage")
    public String invitePage(HttpServletRequest request, ModelMap map) {
        SessionMember session = getSessionMember(request);
        if (session == null) {
            return "/user/login/index.jsp";
        }
        UserInfo userInfo = session.getUser();
        String recommendCode = userInfo.getRecommendCode();
        Long firstCount = serviceFactory.getActivityService().getFirstInviteCount(recommendCode);
        Long secondCount = serviceFactory.getActivityService().getSecondInviteCount(recommendCode);
        Long thirdCount = serviceFactory.getActivityService().getThirdInviteCount(recommendCode);
        /**生成微信分享jssdk所需参数**/
        StringBuffer urlBuffer= request.getRequestURL();
        String url = urlBuffer.toString();
        if (appId != null && !"".equals(appId)) {
            /**
             * 获取accesstoken
             */
            String accessToken = WeixinUtil.getBaseAccessToken(appId, appSecret);
            System.out.println("----------"+appId+"*************"+appSecret);
            
            /**
             * 获取jsapiticket
             */
            String jsapiTicket = WeixinUtil.getBaseJsapiTicket(appId, accessToken);
            /**
             * 生成js 接口 签名信息
             */
            Map<String, String> jsapiValus = WeixinUtil.sign(jsapiTicket, url);
            map.put("appId", appId);
            map.put("url", jsapiValus.get("url"));
            map.put("nonceStr", jsapiValus.get("nonceStr"));
            map.put("timestamp", jsapiValus.get("timestamp"));
            map.put("signature", jsapiValus.get("signature"));
            
        }
        map.addAttribute("recommendCode", recommendCode);
        map.addAttribute("first", firstCount);
        map.addAttribute("second", secondCount);
        map.addAttribute("third", thirdCount);
        map.put("h5domain", h5domain);
        return "/page/account/invitepage.jsp";
    }

    /**
     * 我的资金明细页面
     */
    @RequestMapping(value = "/accountLogPage")
    public String accountLogPage(HttpServletRequest request,ModelMap map){
        SessionMember session = getSessionMember(request);
        if (session==null) {
            return "/user/login/index.jsp";
        }
        UserInfo user = session.getUser();
        UserInfo m = serviceFactory.getUserInfoService().getUserInfoById(user.getId());
        if (StringHelper.isEmpty(m.getTrustAccountId())) {
            return "redirect:/member/goregpnr.htm";
        }
        return "/page/account/accountlog.jsp";
    }
    /**
     * <pre>
     * 我的资金明细
     * </pre>
     * 
     * @param map
     * @param request
     * @param type
     * @param status
     * @param queryTime
     * @return
     */
    @RequestMapping(value = "/myAccountLogList")
    @ResponseBody
    public void myAccountLogList(ModelMap map, HttpServletRequest request, String curPage,PrintWriter printWriter) {
        Map<String , List<?>> dataMap = new HashMap<String, List<?>>();
        List<Integer> pagecount = new ArrayList<Integer>();
        int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
        UserInfo m = getSessionMember(request).getUser();
        ServiceResult<List<AccountLog>> serviceResult = serviceFactory.getAccountService().queryAccountLogList(
                m.getId(), int_curPage, 10);
        pagecount.add(serviceResult.getReturnListSize());
        dataMap.put("list", serviceResult.getReturnValue());
        dataMap.put("pageCount", pagecount);
        Gson gson = new Gson();
        printWriter.write(gson.toJson(dataMap)); 
        printWriter.flush(); 
        printWriter.close();
    }
    /**
     * 跳转到验证开户页面 lugaofei 20151105
     */
    @RequestMapping(value = "/goregpnr")
    public String goRegisterpPNR(HttpServletRequest request,ModelMap map){
        SessionMember session = getSessionMember(request);
        if (session==null) {
            return "/user/login/index.jsp";
        }
        UserInfo user = serviceFactory.getUserInfoService().getUserInfoById(session.getUser().getId());
        map.addAttribute("user", user);
        return "/page/account/regpnr.jsp";
    }
}
